﻿var covericon = "";
var courseData = {};
$(function() {
    showCourseInfo();

    bindSubmit();

    bindUploadify();

    $("#txt_catalog").catalogs({ dataurl: rootpath + "api/course.ashx", parmsdata: { type: "getcatalogs", pid: 0} });
});

function bindUploadify() {
    $("#uploadify").uploadify({
        'uploader': rootpath + 'swf/uploadify.swf',
        'script': 'create.ashx',
        'scriptData': { type: 'uploadcovericon', covericon: covericon, courseid: courseid },
        'cancelImg': rootpath + 'images/cancel.png',
        'buttonImg': rootpath + 'images/browse-files.png',
        'folder': rootpath + 'data/coursepicture',
        'queueID': 'dv_queue',
        'auto': true,
        'multi': false,
        'width': 130,
        'sizeLimit': 1024 * 1024 * 2,
        'fileDesc': '请选择gif jpg pgn bmp jpeg文件',
        'fileExt': '*.gif;*.jpg;*.png;*.bmp;*.jpeg',
        onSelect: function(event, queueid, fileobj) {
            $.ajax({
                type: "GET",
                url: rootpath + "api/course.ashx",
                data: { type: 'createfilename', r: Math.random() },
                dataType: "string",
                async: false,
                success: function(data) {
                    covericon = data + fileobj.type;
                }
            });

            $("#uploadify").uploadifySettings('scriptData', { 'covericon': covericon });
        },
        onOpen: function() {

        },
        onComplete: function(event, queueid, fileobj) {
            $("#dv_preview").empty();
            $("<img/>").css({ width: '160px', height: '120px' }).attr("src", getCourseCover(covericon)).appendTo($("#dv_preview"));
            $("#dv_preview").append("&nbsp;");
            $("<input type='button' class='deleteButton' />").click(function() {
                $.ajax({
                    type: "post",
                    url: "create.ashx",
                    data: { type: 'deletecovericon', filename: covericon },
                    dataType: "string",
                    success: function(data) {
                        if (data == 1) {
                            $("#dv_preview").fadeOut("slow");
                            covericon = "";
                        }
                    }
                });
            }).appendTo($("#dv_preview"));
            $("#dv_preview").fadeIn("slow");
            formloaded();
        },
        onError: function() {
            covericon = "";
        },
        onCancel: function() {
            covericon = "";
            $("#uploadify").uploadifyClearQueue();
        }
    });
}

function showCourseInfo() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcourseinfo", isedit: 1, courseid: courseid, r: Math.random() },
        dataType: "json",
        success: function(data) {
            courseData = data;

            $(":radio[value='" + data.coursetype + "']").attr("checked", "checked");

            $("#txt_coursename").val(data.coursename);
            $("#txt_serialnumber").val(data.serialnumber);

            $("#txt_catalog").val(data.catalogname);
            $("#txt_catalog").attr("rel", data.catalogid);
            $("#txt_playlength").val(data.playlength);
            $("#txt_keywords").val(data.keywords);

            if (data.covericon != "") {
                covericon = data.covericon;

                $("<img/>").css({ width: '160px', height: '120px' }).attr("src", getCourseCover(data.covericon)).appendTo($("#dv_preview"));
                $("#dv_preview").append("&nbsp;");
                $("<input type='button' class='deleteButton' />").click(function() {
                    $.ajax({
                        type: "post",
                        url: "create.ashx",
                        data: { type: 'deletecovericon', filename: covericon, courseid: courseid },
                        dataType: "string",
                        success: function(data) {
                            if (data == 1) {
                                $("#dv_preview").fadeOut("slow");
                                covericon = "";
                            }
                        }
                    });
                }).appendTo($("#dv_preview"));
                $("#dv_preview").show();
            }

            setTimeout("formloaded();setEditorVal();", 1000);
        }
    });
}

function setEditorVal() {
    try {
        descEditor.setData(courseData.description);
        introEditor.setData(courseData.lecturerintro);
    }
    catch (ex) {
        setTimeout("setEditorVal();", 1000);
    }
}

function bindSubmit() {
    $("form").validate({
        rules: {
            txt_coursename: { required: true },
            txt_catalog: { required: true },
            txt_playlength: { digits: true }
        },
        messages: {
            txt_coursename: { required: "课程名称不能为空！" },
            txt_catalog: { required: "课程目录不能为空！" },
            txt_playlength: { digits: "课程播放时长必需为整数！" }
        },
        submitHandler: function() {
            $(".btn").attr("disabled", "disabled").after("<span>提交中.....</span>");

            var ispublic = $(":radio[name='ispublic']").filter(":checked").val();
            var isrecommend = $(":radio[name='isrecommend']").filter(":checked").val();
            var coursename = $("#txt_coursename").val();
            var serialnumber = $("#txt_serialnumber").val();
            var description = descEditor.getData();
            var intro = introEditor.getData();
            var keywords = $("#txt_keywords").val();
            var playlength = $("#txt_playlength").val();
            var catalogid = $("#txt_catalog").attr("rel");
            $.ajax({
                type: "post",
                url: rootpath + "course/create/create.ashx",
                data: {
                    type: 'updatecourse',
                    courseid: courseid,
                    cname: coursename,
                    ispublic: ispublic,
                    isrecommend: isrecommend,
                    description: description,
                    intro: intro,
                    keywords: keywords,
                    playlength: playlength,
                    catalogid: catalogid,
                    curcatalogid: courseData.catalogid,
                    covericon: covericon
                },
                dataType: "string",
                success: function(data) {
                    if (data > 0) {
                        facebox("修改课程成功！");
                        closeDialog();
                    }
                    else {
                        facebox("添加课程失败！");
                    }
                    $(".btn").attr("disabled", "").next("span").remove();
                }
            });
        }
    });
}